package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.AddMarketDto;
import com.codingfuture.erp_system.dto.MarketDetailDto;
import com.codingfuture.erp_system.dto.MarketDto;
import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Orders;
import com.codingfuture.erp_system.entity.Role;
import com.codingfuture.erp_system.entity.Supplier;
import com.codingfuture.erp_system.service.MarketService;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v2")
public class MarketController {

    @Autowired
    private MarketService marketService;

    //数据渲染
    @GetMapping("/market/listByPage")
    public Result findByAll(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows) {
        Page<Orders> page1 = PageHelper.startPage(page, rows);
        List<MarketDto> list = marketService.findByAll();
        for (MarketDto marketDto : list) {
            List<MarketDetailDto> list1 = marketService.findMarketDetailDto(marketDto.getUuid());
            marketDto.setOrderDetailList(list1);
        }
        PageData<MarketDto> data = new PageData<>(list, page1.getTotal());
        return Result.ok(data);
    }

    //新增数据
    @GetMapping("/market/showsupplier")
    public Result showSupplier(Integer type) {
        List<Supplier> list = marketService.showSupplier(type);
        return Result.OK(list);
    }

    @PostMapping("/market/add")
    public Result addMarketDetail(@RequestBody AddMarketDto addMarketDto, HttpSession session) {
        Date date = new Date();
        List<MarketDetailDto> marketDetails = addMarketDto.getMarketDetails();
        Integer customeruuid = addMarketDto.getCustomeruuid();
        MarketDto marketDto = new MarketDto();
        Object name = session.getAttribute("name");
        Role role = marketService.findId(name);
        Integer uuid = role.getUuid();
        Double totalMoney = 0.0;
        for (MarketDetailDto marketDetail : marketDetails) {
            totalMoney += marketDetail.getMoney();
        }
        marketDto.setTotalmoney(totalMoney);
        marketDto.setSupplieruuid(customeruuid);
        marketDto.setCreatetime(date);
        marketDto.setCreater(uuid);
        int num = marketService.addById(marketDto);
        long dtoUuid = marketDto.getUuid();
        for (MarketDetailDto marketDetail : marketDetails) {
            marketDetail.setEndtime(date);
            marketDetail.setOrdersuuid(dtoUuid);
        }
        int i = marketService.addMarketDetail(marketDetails);
        if (num > 0 && i > 0) {
            return Result.ok();
        } else {
            return Result.error(201, "错误");
        }
    }

    //出库
    @PostMapping("/marketdetail/doOutStore")
    public Result doOutStore(Integer id, Integer storeuuid) {
        Date date = new Date();
        Boolean result = marketService.doOutStore(id, date, storeuuid);
        return result ? Result.ok() : Result.error(201, "错误");
    }


}
